/*
 * Copyright (C) 2017-2021
 * All rights reserved, Designed By
 * Copyright authorization contact 18814114118
 */
package com.shop.cereshop.agent.service.notice.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.shop.cereshop.agent.dao.notice.CereNoticeDAO;
import com.shop.cereshop.agent.page.notice.CereNoticeAgentVO;
import com.shop.cereshop.agent.page.user.CereAgentUserVO;
import com.shop.cereshop.agent.service.notice.CereNoticeAgentRelService;
import com.shop.cereshop.agent.service.notice.CereNoticeService;
import com.shop.cereshop.commons.constant.IntegerEnum;
import com.shop.cereshop.commons.domain.common.Page;
import com.shop.cereshop.commons.domain.notice.CereNotice;
import com.shop.cereshop.commons.domain.notice.CereNoticeAgentRel;
import com.shop.cereshop.commons.exception.CoBusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class CereNoticeServiceImpl extends ServiceImpl<CereNoticeDAO, CereNotice> implements CereNoticeService {

  @Autowired
  private CereNoticeDAO cereNoticeDAO;

  @Autowired
  private CereNoticeAgentRelService cereNoticeAgentRelService;

  @Override
  public void insert(CereNotice cereNotice) throws CoBusinessException {

  }

  @Override
  public CereNotice getById(Long noticeId) {
    return cereNoticeDAO.selectByPrimaryKey(noticeId);
  }

  @Override
  public Page<CereNoticeAgentVO> getAll(Integer page, Integer pageSize, Long agentUserId, Integer noticeType,
      Integer receive) {
    PageHelper.startPage(page, pageSize);
    List<CereNoticeAgentVO> producerInfo = cereNoticeDAO.getAll(agentUserId, noticeType, receive);
    PageInfo<CereNoticeAgentVO> pageInfo = new PageInfo<>(producerInfo);
    Page<CereNoticeAgentVO> result = new Page<>(pageInfo.getList(), pageInfo.getTotal());
    return result;
  }

  @Override
  public CereNotice getIndexNotice(Integer receive, CereAgentUserVO user) {
    Long agentUserId = user.getAgentUserId();
    // 查询最新1条系统消息 - 发给该代理商或发给所有代理商的公告
    return lambdaQuery()
        .and(wrapper -> wrapper
            .eq(CereNotice::getAgentUserId, agentUserId)
            .or()
            .isNull(CereNotice::getAgentUserId))
        .eq(CereNotice::getReceive, receive)
        .orderByDesc(CereNotice::getCreateTime)
        .one();
  }

  @Override
  public CereNoticeAgentVO getByIdForAgent(Long noticeId, Long agentUserId) {
    // 根据消息ID和用户ID查询消息及已读状态
    List<CereNoticeAgentVO> list = cereNoticeDAO.getByIdForAgent(noticeId, agentUserId);
    if (list != null && !list.isEmpty()) {
      // 插入已读记录
      CereNoticeAgentRel cereNoticeAgentRel = CereNoticeAgentRel.builder()
              .noticeId(noticeId)
              .agentUserId(agentUserId)
              .ifRead(IntegerEnum.YES.getCode())
              .createTime(LocalDateTime.now())
              .build();
      cereNoticeAgentRelService.save(cereNoticeAgentRel);
      return list.get(0);
    }
    return null;
  }

  @Override
  public CereNoticeAgentVO getIndexNoticeForAgent(Integer receive, CereAgentUserVO user) {
    Long agentUserId = user.getAgentUserId();
    // 查询最新1条消息及已读状态
    List<CereNoticeAgentVO> list = cereNoticeDAO.getIndexNoticeForAgent(agentUserId, receive);
    if (list != null && !list.isEmpty()) {
      return list.get(0);
    }
    return null;
  }

}
